package io.renren.modules.lucky.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.DateUtils;
import io.renren.modules.lucky.dao.LuckyRecordDao;
import io.renren.modules.lucky.dto.LuckyRecordDTO;
import io.renren.modules.lucky.dto.background.LuckyIndexChartsResp;
import io.renren.modules.lucky.dto.background.LuckyIndexReq;
import io.renren.modules.lucky.dto.background.LuckyIndexResp;
import io.renren.modules.lucky.dto.background.ReportReqDto;
import io.renren.modules.lucky.entity.LuckyRecordEntity;
import io.renren.modules.lucky.service.LuckyRecordService;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 招财猫后台记录
 *
 * @author Liang Pei
 * @since 1.0.0 2025-01-06
 */
@Service
public class LuckyRecordServiceImpl extends CrudServiceImpl<LuckyRecordDao, LuckyRecordEntity, LuckyRecordDTO> implements LuckyRecordService {

    @Override
    public QueryWrapper<LuckyRecordEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<LuckyRecordEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public LuckyRecordEntity queryBySessionId(String sessionId) {
        return baseDao.selectOne(new LambdaQueryWrapper<LuckyRecordEntity>().eq(LuckyRecordEntity::getSessionId, sessionId));
    }

    @Override
    public List<LuckyRecordEntity> queryListByParam(ReportReqDto reportReq, String platCode) {
        //时间换算 YYYY-MM-DD 换算为 YYYY-MM-DD 00:00:00 并换算为date类型
        String startTime = reportReq.getStartTime() + " 00:00:00";
        String endTime = reportReq.getEndTime() + " 23:59:59";
        Date startDate = DateUtils.parse(startTime, DateUtils.DATE_TIME_PATTERN);
        Date endDate = DateUtils.parse(endTime, DateUtils.DATE_TIME_PATTERN);

        LambdaQueryWrapper<LuckyRecordEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LuckyRecordEntity::getPlatCode, platCode);
        if (StrUtil.isNotBlank(reportReq.getMediaId())) {
            wrapper.eq(LuckyRecordEntity::getMediaId, reportReq.getMediaId());
        }
        //点位 1-用户离开；2-办理失败；3-办理成功
        if (ObjectUtil.isNotNull(reportReq.getPointNum())) {
            wrapper.eq(LuckyRecordEntity::getPointNum, reportReq.getPointNum());
        }
        wrapper.between(LuckyRecordEntity::getCreateTime, startDate, endDate);

        String queryType = reportReq.getQueryType();
        //查询方式 1-日，2-媒体，3-日+媒体，4-点位，5-日+点位，6-媒体+点位，7-省份，8-日+省份
        switch (queryType) {
            case "1":
                return baseDao.queryByDay(reportReq);
            case "2":
                return baseDao.queryByMedia(reportReq);
            case "3":
                return baseDao.queryByDayAndMedia(reportReq);
            case "4":
                return baseDao.queryByPointNum(reportReq);
            case "5":
                return baseDao.queryByDayAndPointNum(reportReq);
            case "6":
                return baseDao.queryByMediaAndPointNum(reportReq);
            case "7":
                return baseDao.queryByProvince(reportReq);
            case "8":
                return baseDao.queryByDayAndProvince(reportReq);
            default:
                throw new IllegalArgumentException("Invalid query type: " + reportReq.getQueryType());
        }
    }

    @Override
    public LuckyIndexResp queryYesterdayData(String platCode, LuckyIndexReq indexReq) {
        return baseDao.queryYesterdayData(platCode, indexReq.getStartTime(), indexReq.getEndTime());
    }

    @Override
    public LuckyIndexResp querySevenDaysData(String platCode, LuckyIndexReq indexReq) {
        return baseDao.querySevenDaysData(platCode, indexReq.getStartTime(), indexReq.getEndTime());
    }

    @Override
    public LuckyIndexResp queryMonthData(String platCode, LuckyIndexReq indexReq) {
        return baseDao.queryMonthData(platCode, indexReq.getStartTime(), indexReq.getEndTime());
    }

    @Override
    public List<LuckyIndexChartsResp> queryChartsData(String platCode, LuckyIndexReq indexReq) {
        return baseDao.queryByDay(platCode, indexReq.getStartTime(), indexReq.getEndTime());
    }

}